home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / TEXAS XFCNs ƒ / grabText.z1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-29  |  2.3 KB  |  102 lines  |  [TEXT/KAHL]

  1. /* very simple XFCN to grab a chunk of text out of a text file 'filenum'
  2.  * (file must be already opened; pass the refNum to this function),
  3.  * centered on a chosen byte 'target', of a chosen 'size' in bytes
  4.  * ... if the request extends beyond the boundaries of the actual file,
  5.  * just truncate it so that it fits ... call it as:
  6.  *
  7.  *      grabText (target, size, filenum)
  8.  *
  9.  * and when it returns it will give the chunk of text back as an answer.
  10.  * Note that Hypercard doesn't want more than 30kB in any single field,
  11.  * but this function doesn't respect that limit.  Note that if the 'text'
  12.  * file contains '\0' characters, HyperCard will probably be fooled and
  13.  * only accept the chunk of text up to the first '\0' encountered.  Note
  14.  * that the only response to an error is to beep and return empty-handed.
  15.  *
  16.  * For use on the alternative text card on ^z's TEXAS stack.... an XFCN
  17.  * numbered 2088 or so....
  18.  *
  19.  * 871229 - ^z
  20.  */
  21.  
  22. #include <MacTypes.h>
  23. #include <FileMgr.h>
  24. #include <HyperXCmd.h>
  25.  
  26. pascal void main (XCmdBlockPtr paramPtr);
  27. long atol (char *s);
  28.  
  29. pascal void main (paramPtr)
  30.   XCmdBlockPtr paramPtr;
  31.   {
  32.     int refNum, err;
  33.     long target, size, offset, bytes;
  34.     Handle answer;
  35.     
  36.     if (paramPtr->paramCount != 3)
  37.       {
  38.           SysBeep (10);
  39.           return;
  40.       }
  41.     target = atol (*(paramPtr->params[0]));
  42.     size = atol (*(paramPtr->params[1]));
  43.     refNum = atol (*(paramPtr->params[2]));
  44.     if (target < 0 || size < 1 || refNum == 0)
  45.       {
  46.           SysBeep (10);
  47.           return;
  48.       }
  49.     offset = target - size / 2;
  50.     if (offset < 0)
  51.         offset = 0;
  52.     bytes = size;
  53.     answer = NewHandle (size + 1);
  54.     if (answer == 0)
  55.       {
  56.           SysBeep (10);
  57.         return;
  58.       }
  59.     HLock (answer);
  60.     SetFPos (refNum, fsFromStart, offset);
  61.     err = FSRead (refNum, &bytes, *answer);
  62.     if (err != noErr && err != eofErr)
  63.       {
  64.           SysBeep (10);
  65.         return;    
  66.       }
  67.     *(*answer + bytes) = '\0';
  68.     paramPtr->returnValue = answer;
  69.     HUnlock (answer);
  70.     return;
  71.   }
  72.   
  73.   
  74. /* function to convert alphabetic string to a long integer ... from LSC
  75.  * library.... simplified to avoid using isspace() & isdigit() ....
  76.  */
  77.  
  78. long atol (s)
  79.   register char *s;
  80.   {
  81.     register char signflag = 0;
  82.     register long r = 0;
  83.  
  84.     while ((*s == ' '))
  85.         s++;
  86.         
  87.     if (*s == '-')
  88.       {
  89.         signflag = 1;
  90.         s++;
  91.       }
  92.     else if (*s == '+')
  93.          s++;
  94.  
  95.     while (*s >= '0' && *s <= '9') 
  96.         r = r * 10 + (*s++ - '0');
  97.     
  98.     return (signflag ? -r : r);
  99. }
  100.  
  101.  
  102.